package top.humbleyuan.dfs;

/**
 * @Author HumbleYuan
 * @Date 2020/5/18 20:32
 * @Des 岛屿数量
 * @pro 待补充并查集方法
 */
public class LeetCode_200 {
    public static void main(String[] args) {
        char[][] nums = {{'1','1','0','0','0'},{'1','1','0','0','0'},{'0','0','1','0','0'},{'0','0','0','1','1'}};

        if(nums == null || nums.length == 0 || nums[0].length == 0) {
            System.out.println(0);
            return;
        }

        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums[i].length; j++) {
                if(nums[i][j] == '1') {
                    count++;

                    dfs(nums, i, j);
                }
            }
        }

        System.out.println(count);
    }

    public static void dfs(char[][] nums, int x, int y) {
        if(x < 0 || x >= nums.length || y < 0 || y >= nums[0].length || nums[x][y] == '0') {
            return;
        }

        nums[x][y] = '0';
        // dfs
        dfs(nums, x, y - 1);
        dfs(nums, x, y + 1);
        dfs(nums, x - 1, y);
        dfs(nums, x + 1, y);

    }
}
